//
//  PhotoCollectionViewCell.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2018/12/27.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "PhotoCollectionViewCell.h"
//#import <DACircularProgress/DACircularProgressView.h>
#import "MWCommon.h"
#import "MWPhotoBrowserPrivate.h"
#import "UIImage+MWPhotoBrowser.h"

#define VIDEO_INDICATOR_PADDING 10

@interface PhotoCollectionViewCell () {
    
    UIImageView *_imageView;
    UIImageView *_videoIndicator;
    UIImageView *_loadingError;
    UILabel *_timeLabel;
//    DACircularProgressView *_loadingIndicator;
    UIButton *_selectedButton;
    
}

@end
@implementation PhotoCollectionViewCell

- (void)displayCornerRadius:(BOOL)display {
    if (display) {
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = 10;
    }else {
        _imageView.layer.masksToBounds = NO;
        _imageView.layer.cornerRadius = 0;
    }
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        
        // Grey background
        self.backgroundColor = [UIColor whiteColor];
        
        // Image
        _imageView = [UIImageView new];
        _imageView.frame = self.bounds;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
        
        _imageView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self addSubview:_imageView];

        _videoIndicator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alb_small_video"]];
        _videoIndicator.frame = CGRectMake(0, 0, 35, 35);
        _videoIndicator.center = _imageView.center;
        
        [self addSubview:_videoIndicator];
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0,CGRectGetHeight(_imageView.frame) - 20,CGRectGetWidth(_imageView.frame)-10,20);
        label.textAlignment = NSTextAlignmentRight;
        label.textColor = RGB16(0xffffff);
        label.font = [UIFont systemFontOfSize:11];
//        label.text = @"11:23:58";
        _timeLabel = label;
        [self addSubview:_timeLabel];
        
        // Selection button
        _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectedButton.contentMode = UIViewContentModeTopRight;
        _selectedButton.adjustsImageWhenHighlighted = NO;
        [_selectedButton setImage:[UIImage imageForResourcePath:@"MWPhotoBrowser.bundle/ImageSelectedSmallOff" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateNormal];
        [_selectedButton setImage:[UIImage imageForResourcePath:@"MWPhotoBrowser.bundle/ImageSelectedSmallOn" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]] forState:UIControlStateSelected];
        [_selectedButton addTarget:self action:@selector(selectionButtonPressed) forControlEvents:UIControlEventTouchDown];
        _selectedButton.hidden = YES;
        _selectedButton.frame = CGRectMake(0, 0, 38, 38);
        [self addSubview:_selectedButton];
        
        // Loading indicator
//        _loadingIndicator = [[DACircularProgressView alloc] initWithFrame:CGRectMake(0, 0, 40.0f, 40.0f)];
//        _loadingIndicator.userInteractionEnabled = NO;
//        _loadingIndicator.thicknessRatio = 0.1;
//        _loadingIndicator.roundedCorners = NO;
//        [self addSubview:_loadingIndicator];
        
        // Listen for photo loading notifications
//        [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(setProgressFromNotification:)
//                                                     name:MWPHOTO_PROGRESS_NOTIFICATION
//                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleMWPhotoLoadingDidEndNotification:)
                                                     name:MWPHOTO_LOADING_DID_END_NOTIFICATION
                                                   object:nil];
        
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setGridController:(PhotoViewController *)gridController {
    _gridController = gridController;
}

#pragma mark - View

- (void)layoutSubviews {
    [super layoutSubviews];
    _imageView.frame = self.bounds;
//    _loadingIndicator.frame = CGRectMake(floorf((self.bounds.size.width - _loadingIndicator.frame.size.width) / 2.),
//                                         floorf((self.bounds.size.height - _loadingIndicator.frame.size.height) / 2),
//                                         _loadingIndicator.frame.size.width,
//                                         _loadingIndicator.frame.size.height);
    _selectedButton.frame = CGRectMake(self.bounds.size.width - _selectedButton.frame.size.width,
                                       self.bounds.size.height - _selectedButton.frame.size.width, _selectedButton.frame.size.width, _selectedButton.frame.size.height);
}

#pragma mark - Cell

- (void)prepareForReuse {
    _photoModel = nil;
    _gridController = nil;
    _imageView.image = nil;
//    _loadingIndicator.progress = 0;
    _selectedButton.hidden = YES;
    [self hideImageFailure];
    [super prepareForReuse];
}

#pragma mark - Image Handling

- (void)setPhotoModel:(PhotoModel *)photoModel {
    _photoModel = photoModel;
    MWPhoto *photo = _photoModel.mwPhoto;
    UIImage *img = nil;
    if ([photo underlyingImage]) {
        img = [photo underlyingImage];
    }
    if (img) {
        [self displayImage];
    } else {
        [photo loadUnderlyingImageAndNotify];
    }
    [self displayCornerRadius:self.isNormalStyle];
    if (self.isNormalStyle && _photoModel.isVideo) {
        _videoIndicator.hidden = NO;
        _timeLabel.hidden = NO;
        _timeLabel.text = _photoModel.duration;
    }else {
        _videoIndicator.hidden = YES;
        _timeLabel.hidden = YES;
    }
}



- (void)displayImage {
    _imageView.image = [_photoModel.mwPhoto underlyingImage];
    _selectedButton.hidden = !_selectionMode;
    [self hideImageFailure];
}

#pragma mark - Selection

- (void)setSelectionMode:(BOOL)selectionMode {
    _selectionMode = selectionMode;
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    _selectedButton.selected = isSelected;
}

- (void)selectionButtonPressed {
    _selectedButton.selected = !_selectedButton.selected;
    _photoModel.isSelected = _selectedButton.selected;
//    [_gridController.browser setPhotoSelected:_selectedButton.selected atIndex:_index];
}

#pragma mark - Touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _imageView.alpha = 0.6;
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    _imageView.alpha = 1;
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    _imageView.alpha = 1;
    [super touchesCancelled:touches withEvent:event];
}

#pragma mark Indicators

- (void)hideLoadingIndicator {
//    _loadingIndicator.hidden = YES;
}

- (void)showLoadingIndicator {
//    _loadingIndicator.progress = 0;
//    _loadingIndicator.hidden = NO;
    [self hideImageFailure];
}

- (void)showImageFailure {
    // Only show if image is not empty
    if (![_photoModel.mwPhoto respondsToSelector:@selector(emptyImage)] || !_photoModel.mwPhoto.emptyImage) {
        if (!_loadingError) {
            _loadingError = [UIImageView new];
            _loadingError.image = [UIImage imageForResourcePath:@"MWPhotoBrowser.bundle/ImageError" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
            _loadingError.userInteractionEnabled = NO;
            [_loadingError sizeToFit];
            [self addSubview:_loadingError];
        }
        _loadingError.frame = CGRectMake(floorf((self.bounds.size.width - _loadingError.frame.size.width) / 2.),
                                         floorf((self.bounds.size.height - _loadingError.frame.size.height) / 2),
                                         _loadingError.frame.size.width,
                                         _loadingError.frame.size.height);
    }
    [self hideLoadingIndicator];
    _imageView.image = nil;
}

- (void)hideImageFailure {
    if (_loadingError) {
        [_loadingError removeFromSuperview];
        _loadingError = nil;
    }
}

#pragma mark - Notifications

//- (void)setProgressFromNotification:(NSNotification *)notification {
//    dispatch_async(dispatch_get_main_queue(), ^{
//        NSDictionary *dict = [notification object];
//        id <MWPhoto> photoWithProgress = [dict objectForKey:@"photo"];
//        if (photoWithProgress == self->_photo) {
//            //            NSLog(@"%f", [[dict valueForKey:@"progress"] floatValue]);
//            float progress = [[dict valueForKey:@"progress"] floatValue];
//            self->_loadingIndicator.progress = MAX(MIN(1, progress), 0);
//        }
//    });
//}

- (void)handleMWPhotoLoadingDidEndNotification:(NSNotification *)notification {
    id <MWPhoto> photo = [notification object];
    if (photo == _photoModel.mwPhoto) {
        if ([photo underlyingImage]) {
            // Successful load
            [self displayImage];
        } else {
            // Failed to load
            [self showImageFailure];
        }
        [self hideLoadingIndicator];
    }
}

@end
